# -*- coding: UTF-8 -*-
from django.core import template_loader
from django.core.extensions import DjangoContext as Context
from django.core.exceptions import Http404
from django.models.comments import comments, moderatordeletions, userflags
from django.views.decorators.auth import login_required
from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect
from django.conf.settings import SITE_ID

def flag(request, comment_id):
    """
    Signale un commentaire. Si la méthode de la requête est GET, on demande confirmation;
    si la méthode de la requête est POST, on agit.

    Modèles : `comments/flag_verify`, `comments/flag_done`
    Contexte :
        comment
            l'objet `comments.comments` à signaler
    """
    try:
        comment = comments.get_object(id__exact=comment_id, site_id__exact=SITE_ID)
    except comments.CommentDoesNotExist:
        raise Http404
    if request.POST:
        userflags.flag(comment, request.user)
        return HttpResponseRedirect('%sdone/' % request.path)
    t = template_loader.get_template('comments/flag_verify')
    c = Context(request, {
        'comment': comment,
    })
    return HttpResponse(t.render(c))
flag = login_required(flag)

def flag_done(request, comment_id):
    try:
        comment = comments.get_object(id__exact=comment_id, site_id__exact=SITE_ID)
    except comments.CommentDoesNotExist:
        raise Http404
    t = template_loader.get_template('comments/flag_done')
    c = Context(request, {
        'comment': comment,
    })
    return HttpResponse(t.render(c))

def delete(request, comment_id):
    """
    Supprime un commentaire. Si la méthode de la requête est GET, on demande confirmation;
    si la méthode de la requête est POST, on agit.

    Modèles : `comments/delete_verify`, `comments/delete_done`
    Contexte :
        comment
            l'objet `comments.comments` à signaler
    """
    try:
        comment = comments.get_object(id__exact=comment_id, site_id__exact=SITE_ID)
    except comments.CommentDoesNotExist:
        raise Http404
    if not comments.user_is_moderator(request.user):
        raise Http404
    if request.POST:
        # Si le commentaire a déjà été supprimé, on échoue de manière silencieuse.
        if not comment.is_removed:
            comment.is_removed = True
            comment.save()
            m = moderatordeletions.ModeratorDeletion(None, request.user.id, comment.id, None)
            m.save()
        return HttpResponseRedirect('%sdone/' % request.path)
    t = template_loader.get_template('comments/delete_verify')
    c = Context(request, {
        'comment': comment,
    })
    return HttpResponse(t.render(c))
delete = login_required(delete)

def delete_done(request, comment_id):
    try:
        comment = comments.get_object(id__exact=comment_id, site_id__exact=SITE_ID)
    except comments.CommentDoesNotExist:
        raise Http404
    t = template_loader.get_template('comments/delete_done')
    c = Context(request, {
        'comment': comment,
    })
    return HttpResponse(t.render(c))
